﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZeroBug.EasyFlow.Core;

namespace EasyFlow
{
    public partial class VariableJudgeViewModel : ObservableObject
    {
        private readonly VariableJudge varJudge;
        private VariableTreeView _variableTreeView;

        private string _compareVariableExpression;
        public string CompareVariableExpression
        {
            get => _compareVariableExpression;
            set 
            { 
                SetProperty<string>(ref _compareVariableExpression, value);
                varJudge.SourceVariable.Expressions = value.Split(',').ToList();
            }
        }

        private string _compareValueExpression;
        public string CompareValueExpression
        {
            get => _compareValueExpression;
            set
            {
                SetProperty<string>(ref _compareValueExpression, value);
                varJudge.CompareVariable.Expressions = value.Split(',').ToList();
            }
        }

        private CompareSign _compareSign;
        public CompareSign CompareSign
        {
            get => varJudge.Sign;
            set
            {
                SetProperty<CompareSign>(ref _compareSign, value);
                varJudge.Sign = _compareSign;
            }
        }
        private int _compareIndex;
        public int CompareIndex
        {
            get => varJudge.CompareIndex;
            set
            {
                SetProperty<int>(ref _compareIndex, value);
                varJudge.CompareIndex = _compareIndex;
            }
        }

        public VariableJudgeViewModel(VariableJudge varJudge)
        {
            this.varJudge = varJudge;
            this._compareVariableExpression = string.Join(',', varJudge.SourceVariable.Expressions);
            this._compareValueExpression = string.Join(',', varJudge.CompareVariable.Expressions);
        }
        [RelayCommand]
        void CompareVariableLinkButtonClick()
        {
            if (_variableTreeView != null && _variableTreeView.IsInitialized && _variableTreeView.IsVisible)
            {
                if (_variableTreeView.WindowState == System.Windows.WindowState.Minimized)
                    _variableTreeView.WindowState = System.Windows.WindowState.Normal;
                _variableTreeView.Activate();
            }
            else
            {
                _variableTreeView = new VariableTreeView();
                _variableTreeView.DataContext = new VariableManagerViewModel(App.Current.Services.GetService<FlowManager>());
                _variableTreeView.AppendVariableCommand = AppendVariableCommand;
                _variableTreeView.ReplaceVariableCommand = ReplaceVariableCommand;
                _variableTreeView.Show();
            }
        }
        [RelayCommand]
        void CompareValueLinkButtonClick()
        {
            if (_variableTreeView != null && _variableTreeView.IsInitialized && _variableTreeView.IsVisible)
            {
                if (_variableTreeView.WindowState == System.Windows.WindowState.Minimized)
                    _variableTreeView.WindowState = System.Windows.WindowState.Normal;
                _variableTreeView.Activate();
            }
            else
            {
                _variableTreeView = new VariableTreeView();
                _variableTreeView.DataContext = new VariableManagerViewModel(App.Current.Services.GetService<FlowManager>());
                _variableTreeView.AppendVariableCommand = AppendValueCommand;
                _variableTreeView.ReplaceVariableCommand = ReplaceValueCommand;
                _variableTreeView.Show();
            }
        }
        [RelayCommand]
        void AppendVariable(object obj)
        {
            if (obj is VariableViewModel)
            {
                VariableViewModel vvm = obj as VariableViewModel;
                if (!string.IsNullOrEmpty(CompareVariableExpression))
                    CompareVariableExpression += $",@[{vvm.BindingVariable.Name}]";
                varJudge.SourceVariable.Expressions = CompareVariableExpression.Split(',').ToList();
            }
        }
        [RelayCommand]
        void ReplaceVariable(object obj)
        {
            if (obj is VariableViewModel)
            {
                VariableViewModel vvm = obj as VariableViewModel;
                CompareVariableExpression = $"@[{vvm.BindingVariable.Name}]";
                varJudge.SourceVariable.Expressions = new List<string>() { CompareVariableExpression };
                //if (!vvm.IsNodeOnly)
                //{
                //    (_variableTreeView.DataContext as VariableManagerViewModel).SelectedVariableName = vvm.Name;
                //    //VarExp = vvm.Name;
                //}
            }
        }
        [RelayCommand]
        void AppendValue(object obj)
        {
            if (obj is VariableViewModel)
            {
                VariableViewModel vvm = obj as VariableViewModel;
                if (!string.IsNullOrEmpty(CompareValueExpression))
                    CompareValueExpression += $",@[{vvm.BindingVariable.Name}]";
                varJudge.CompareVariable.Expressions = CompareValueExpression.Split(',').ToList();
            }
        }
        [RelayCommand]
        void ReplaceValue(object obj)
        {
            if (obj is VariableViewModel)
            {
                VariableViewModel vvm = obj as VariableViewModel;
                CompareValueExpression = $"@[{vvm.BindingVariable.Name}]";
                varJudge.CompareVariable.Expressions = new List<string>() { CompareValueExpression };
                //if (!vvm.IsNodeOnly)
                //{
                //    (_variableTreeView.DataContext as VariableManagerViewModel).SelectedVariableName = vvm.Name;
                //    //VarExp = vvm.Name;
                //}
            }
        }
    }
}
